package edu.xidian.bdilab.jrbigdata.controller.enterprise;

import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.service.enterprise.register.RegisterService;
import edu.xidian.bdilab.jrbigdata.util.ConvertString;
import edu.xidian.bdilab.jrbigdata.util.DecryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName RegisterController
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/3/25 上午8:42
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@CrossOrigin
@Controller
@Api(tags = {"enterpriseRegister-企业用户注册功能"})
public class RegisterController {
    @Autowired
    public RegisterService registerServiceImpl;
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "socialCode", value = "统一社会信用代码（密码）"),
            @ApiImplicitParam(name = "enterpriseName", value = "企业名称"),
            @ApiImplicitParam(name = "registeredCaptial", value = "企业注册资本（单位：万元）"),
            @ApiImplicitParam(name = "ownerName", value = "法人姓名"),
            @ApiImplicitParam(name = "region", value = "所属区域(枚举)"),
            @ApiImplicitParam(name = "street", value = "街道/乡镇"),
            @ApiImplicitParam(name = "ownerPhone", value = "法定代表人手机号码"),
            @ApiImplicitParam(name = "contactName", value = "联系人姓名"),
            @ApiImplicitParam(name = "contactPhone", value = "联系人手机号"),
            @ApiImplicitParam(name = "establishTime", value = "成立时间（yyyy-mm-dd）"),
            @ApiImplicitParam(name = "establishAddress", value = "注册地址"),
            @ApiImplicitParam(name = "fundQuota", value = "信保基金额度（单位：万元）"),
            @ApiImplicitParam(name = "available", value = "可用额度（单位：万元）"),
            @ApiImplicitParam(name = "businessScop", value = "经营范围"),
            @ApiImplicitParam(name = "enterpriseDesc", value = "企业详情"),
            @ApiImplicitParam(name = "accountBank", value = "开户行"),
            @ApiImplicitParam(name = "electricityChargeCode", value = "水电缴费账号"),
            @ApiImplicitParam(name = "socialSecurityCode", value = "社保缴费账号"),
            @ApiImplicitParam(name = "password", value = "密码"),
    })
    @RequestMapping(value = "enterprise/register", method = RequestMethod.POST)
    public ResponseResult register(@RequestParam String socialCode,
                                   @RequestParam String enterpriseName,
                                   @RequestParam int registeredCaptial,
                                   @RequestParam String ownerName,
                                   @RequestParam int region,
                                   @RequestParam String street,
                                   @RequestParam String ownerPhone,
                                   @RequestParam String contactName,
                                   @RequestParam String contactPhone,
                                   @RequestParam(required = false) String establishTime,
                                   @RequestParam(required = false) String establishAddress,
                                   @RequestParam(required = false) String fundQuota,
                                   @RequestParam(required = false) String available,
                                   @RequestParam(required = false) String businessScop,
                                   @RequestParam(required = false) String enterpriseDesc,
                                   @RequestParam(required = false) String accountBank,
                                   @RequestParam(required = false) String electricityChargeCode,
                                   @RequestParam(required = false) String socialSecurityCode,
                                   @RequestParam String password) {
        ResponseResult responseResult = new ResponseResult();

        /**
         * 日期转化
          */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date f_establish_time = null;

        try {
            f_establish_time = sdf.parse(establishTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ConvertString convertString = new ConvertString();

        //查到了返回false，没查到返回true
        if (registerServiceImpl.checkEnterpriseInfo(socialCode)){
            //没查到
            registerServiceImpl.setEnterpriseInfo(socialCode, enterpriseName,
                    registeredCaptial, ownerName,
                    region, street,
                    ownerPhone, contactName,
                    contactPhone,f_establish_time,
                    establishAddress, convertString.StringToInt(fundQuota),
                    convertString.StringToInt(available), businessScop,
                    enterpriseDesc, accountBank,
                    electricityChargeCode, socialSecurityCode);

            //密码转化
            String md5Password = DecryptUtils.getMd5(password);
            registerServiceImpl.addEnterpriseUser(socialCode, md5Password);

            responseResult.setSuccess(true);
            responseResult.setCode(EnterpriseCode.REGISTER_SUCCESS.getCode());
            responseResult.setMessage(EnterpriseCode.REGISTER_SUCCESS.getMessage());

        }else {
            //已经存在用户
            responseResult.setSuccess(false);
            responseResult.setCode(EnterpriseCode.REGISTER_FAIL.getCode());
            responseResult.setMessage(EnterpriseCode.REGISTER_FAIL.getMessage());
        }

        return responseResult;
    }
}
